#include "StdAfx.h"

TutorialFrame::TutorialFrame(ui32 x, ui32 y, ui32 cx, ui32 cy) : m_title("Fonts\\FrizQT__.ttf", "Friz Quadrata TT", 7, 15)
{
	m_visible = true;
	m_moveAble = false;
	m_position.x = (float)x;
	m_position.y = (float)y;
	m_size.x = (float)cx;
	m_size.y = (float)cy;
	m_baseFrame = CTextureHandler::GetInstance()->GetTexture("Interface\\TUTORIALFRAME\\UI-HELP-FRAME-POPUP.blp");
	m_circleIcon = CTextureHandler::GetInstance()->GetTexture("Interface\\TUTORIALFRAME\\UI-HELP-PORTRAIT.blp");
	m_closeIcon = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up.blp");
	m_closeIconDown = CTextureHandler::GetInstance()->GetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down.blp");
	m_closeHovered = false;

	m_baseVertices[0].z = m_baseVertices[1].z = m_baseVertices[2].z = m_baseVertices[3].z = 0.0f;
	m_baseVertices[0].rhw = m_baseVertices[1].rhw = m_baseVertices[2].rhw = m_baseVertices[3].rhw = 1.0f;
	m_baseVertices[0].x = (float)x;
	m_baseVertices[0].y = (float)y;
	m_baseVertices[0].u = 0.0f;
	m_baseVertices[0].v = 0.0f;

	m_baseVertices[1].x = (float)x + cx;
	m_baseVertices[1].y = (float)y;
	m_baseVertices[1].u = 0.73f;
	m_baseVertices[1].v = 0.0f;

	m_baseVertices[2].x = (float)x + cx;
	m_baseVertices[2].y = (float)y + cy;
	m_baseVertices[2].u = 0.73f;
	m_baseVertices[2].v = 0.73f;

	m_baseVertices[3].x = (float)x;
	m_baseVertices[3].y = (float)y + cy;
	m_baseVertices[3].u = 0.0f;
	m_baseVertices[3].v = 0.73f;

	m_iconVertices[0].z = m_iconVertices[1].z = m_iconVertices[2].z = m_iconVertices[3].z = 0.0f;
	m_iconVertices[0].rhw = m_iconVertices[1].rhw = m_iconVertices[2].rhw = m_iconVertices[3].rhw = 1.0f;
	m_iconVertices[0].x = (float)x + 8;
	m_iconVertices[0].y = (float)y + 6;
	m_iconVertices[0].u = 0.0f;
	m_iconVertices[0].v = 0.0f;

	m_iconVertices[1].x = (float)x + 8 + 64;
	m_iconVertices[1].y = (float)y + 6;
	m_iconVertices[1].u = 1.0f;
	m_iconVertices[1].v = 0.0f;

	m_iconVertices[2].x = (float)x + 8 + 64;
	m_iconVertices[2].y = (float)y + 6 + 64;
	m_iconVertices[2].u = 1.0f;
	m_iconVertices[2].v = 1.0f;

	m_iconVertices[3].x = (float)x + 8;
	m_iconVertices[3].y = (float)y + 6 + 64;
	m_iconVertices[3].u = 0.0f;
	m_iconVertices[3].v = 1.0f;

	m_closeVertices[0].z = m_closeVertices[1].z = m_closeVertices[2].z = m_closeVertices[3].z = 0.0f;
	m_closeVertices[0].rhw = m_closeVertices[1].rhw = m_closeVertices[2].rhw = m_closeVertices[3].rhw = 1.0f;
	m_closeVertices[0].x = (float)x + 337;
	m_closeVertices[0].y = (float)y + 9;
	m_closeVertices[0].u = 0.0f;
	m_closeVertices[0].v = 0.0f;

	m_closeVertices[1].x = (float)x + 337 + 32;
	m_closeVertices[1].y = (float)y + 9;
	m_closeVertices[1].u = 1.0f;
	m_closeVertices[1].v = 0.0f;

	m_closeVertices[2].x = (float)x + 337 + 32;
	m_closeVertices[2].y = (float)y + 9 + 32;
	m_closeVertices[2].u = 1.0f;
	m_closeVertices[2].v = 1.0f;

	m_closeVertices[3].x = (float)x + 337;
	m_closeVertices[3].y = (float)y + 9 + 32;
	m_closeVertices[3].u = 0.0f;
	m_closeVertices[3].v = 1.0f;
}

void TutorialFrame::Draw()
{
	if(!m_visible)
		return;
	sD3D->SetFVF(VERTEX3FRHWT);
	sD3D->SetTexture(0, *m_circleIcon);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_iconVertices, sizeof(Vertex3FRHWT));
	sD3D->SetTexture(0, *m_baseFrame);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_baseVertices, sizeof(Vertex3FRHWT));
	if(!m_closeHovered)
		sD3D->SetTexture(0, *m_closeIcon);
	else
		sD3D->SetTexture(0, *m_closeIconDown);
	sD3D->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, m_closeVertices, sizeof(Vertex3FRHWT));
	m_title.DrawTextA(ui32(m_position.x + 190.0f - m_title.GetTextLen(szTitle, true) / 2), ui32(m_position.y + 17), 300, 30, szTitle, 0xFFE5B200);
	std::vector<std::string>::iterator itr = m_lines.begin();
	std::vector<std::string>::iterator end = m_lines.end();

	for(ui32 i = 0; itr != end; ++itr, ++i)
	{
		m_title.DrawTextA((ui32)m_position.x + 30, (ui32)m_position.y + 75 + (i * m_title.GetFontHeight(true) + 2), m_title.GetTextLen(*itr, false), 30, (*itr));
	}
}

void TutorialFrame::OnMouseMove(ui32 x, ui32 y, bool lDown, bool rDown)
{
	if(m_moveAble)
	{
		m_position.x += x - m_moveLast.x;
		m_position.y += y - m_moveLast.y;
		for(ui32 i = 0; i < 4; ++i)
		{
			m_baseVertices[i].x += x - m_moveLast.x;
			m_baseVertices[i].y += y - m_moveLast.y;
			m_closeVertices[i].x += x - m_moveLast.x;
			m_closeVertices[i].y += y - m_moveLast.y;
			m_iconVertices[i].x += x - m_moveLast.x;
			m_iconVertices[i].y += y - m_moveLast.y;
		}
		m_moveLast.x = (float)x;
		m_moveLast.y = (float)y;
	}
}

void TutorialFrame::MouseActivity(bool left, Vertex2F pos, bool up)
{
	if(pos.x - m_position.x >= 341 && pos.x - m_position.x <= (337 + 25) && pos.y - m_position.y >= 15 && pos.y - m_position.y <= (9 + 25))
	{
		if(m_closeHovered && up)
			m_visible = false;
		m_closeHovered = !up;
		return;
	}
	else if(up)
	{
		if(m_closeHovered)
			m_visible = false;
		m_closeHovered = false;
		m_moveAble = false;
	}
	else if(pos.x - m_position.x >= 70 && pos.x - m_position.x <= 339 && pos.y - m_position.y >= 14 && pos.y - m_position.y <= 33)
	{
		m_moveLast = pos;
		m_moveAble = true;
	}
}